﻿Imports System.IO
Public Class EmoteEditor
    Inherits System.Windows.Forms.Form
    Implements IMessageFilter
    Private WM_KEYDOWN As Integer = &H100
    Private WM_KEYUP As Integer = &H101

    Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
        Dim keyCode As Keys = CType(CInt(m.WParam), Keys) And Keys.KeyCode
        If m.Msg = WM_KEYDOWN And keyCode = Keys.Escape Then
            Me.Close()
            Application.RemoveMessageFilter(Me) : Return True
        End If
        Return False
    End Function 'PreFilterMessage
    Public Function GetFileContents(ByVal FullPath As String, _
         Optional ByRef ErrInfo As String = "") As String

        Dim strContents As String
        Dim objReader As StreamReader
        Try
            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            Return strContents
            objReader.Close()
            objReader.Dispose()
        Catch Ex As Exception
            ErrInfo = Ex.Message
            MsgBox(ErrInfo, MsgBoxStyle.Critical, "ERROR!")
            Return ""
        End Try
    End Function

    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean

        Dim bAns As Boolean = False
        Dim trynum As Short = 1
        Dim objReader As StreamWriter
        While bAns = False
            Try
                objReader = New StreamWriter(FullPath)
                objReader.Write(strData)
                objReader.Close()
                bAns = True
            Catch Ex As Exception
                ErrInfo = Ex.Message
                If trynum = 1000 Then
                    MsgBox(ErrInfo, MsgBoxStyle.Critical, "ERROR!")
                    bAns = True
                End If
            End Try
        End While
        Return bAns
    End Function
    Dim EmoteData As String
    Dim EmoteData_EMOTE As String
    Dim EmoteData_PERSE As String
    Dim EmoteData_TARGE As String
    Dim EmoteData_PROPE As String
    Dim EmoteData_HASTA As String
    Dim EmoteData_TARGD As String
    Dim EmoteData_PERSD As String
    Dim EmoteData_ENDTM As String
    Dim EmoteData_TARGR As String
    Dim serr As String
    Dim bans As Boolean
    Private Sub targetchange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsesTargetCheck.CheckedChanged
        If UsesTargetCheck.Checked = True Then
            EmotePersD.Enabled = True
            EmoteTargD.Enabled = True
            Emotepropt.Enabled = True
        Else
            EmotePersD.Enabled = False
            EmoteTargD.Enabled = False
            Emotepropt.Enabled = False
        End If
    End Sub

    Private Sub loademotes(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(Me)
        emotelist.Items.Clear()
        Dim int2 As Integer
        Dim int3 As Integer
        Dim CurNum As Integer = 1
        Dim str1 As String = Replace(Str(CurNum), " ", "")
        EmoteData = GetFileContents(Debugger.DirEmotes, serr)
        int2 = InStr(EmoteData, "EMOTE#" + str1 + "(")
        While int2 > 0
            For int3 = int2 To Len(EmoteData) Step 1
                If Mid(EmoteData, int3, 1) = ")" Then
                    emotelist.Items.Add(LTrim(Str(CurNum)) + ". " + Mid(EmoteData, int2 + 7 + Len(str1), int3 - int2 - 7 - Len(str1)))
                    CurNum += 1
                    str1 = Replace(Str(CurNum), " ", "")
                    Exit For
                End If
            Next
            int2 = InStr(EmoteData, "EMOTE#" + str1 + "(")
        End While
        emotelist.SelectedIndex = 1
    End Sub

    Private Sub loademote(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles emotelist.SelectedIndexChanged
        Dim Emote As String = emotelist.Items.Item(emotelist.SelectedIndex)
        If Emote <> "" Then
            Dim tmp0 As Integer
            For tmp0 = 1 To Len(Emote) Step 1
                If Mid(Emote, tmp0, 1) = "." Then
                    Emote = Mid(Emote, 1, tmp0 - 1)
                End If
            Next
            EmoteData = GetFileContents(Debugger.DirEmotes, serr)
            If serr = "" Then
                Dim tempvar As Integer = InStr(EmoteData, "EMOTE#")
                Dim tempvar2 As Integer
                Dim EmoteVIRID As String = "3"
                If tempvar > 0 Then
                    tempvar = InStr(EmoteData, "(" + Emote + ")")
                    For tempvar2 = InStr(EmoteData, "(" + Emote + ")") To Len(EmoteData) Step 1
                        If Mid(EmoteData, tempvar2, 1) = "#" Then
                            Dim aint As Integer
                            For aint = tempvar2 To Len(EmoteData) Step 1
                                If Mid(EmoteData, aint, 1) = "(" Then
                                    EmoteVIRID = Mid(EmoteData, tempvar2 + 1, aint - tempvar2 - 1)
                                    Exit For
                                End If
                            Next
                            Exit For
                        End If
                    Next
                    tempvar = InStr(EmoteData, "EMOTE#" + EmoteVIRID + "(")
                    For tempvar2 = InStr(EmoteData, "EMOTE#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                        If Mid(EmoteData, tempvar2, 1) = ")" Then
                            EmoteData_EMOTE = Mid(EmoteData, InStr(EmoteData, "EMOTE#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                            Exit For
                        End If
                    Next
                    tempvar = InStr(EmoteData, "PERSE#" + EmoteVIRID + "(")
                    For tempvar2 = InStr(EmoteData, "PERSE#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                        If Mid(EmoteData, tempvar2, 1) = ")" Then
                            EmoteData_PERSE = Mid(EmoteData, InStr(EmoteData, "PERSE#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                            Exit For
                        End If
                    Next
                    tempvar = InStr(EmoteData, "TARGE#" + EmoteVIRID + "(")
                    For tempvar2 = InStr(EmoteData, "TARGE#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                        If Mid(EmoteData, tempvar2, 1) = ")" Then
                            EmoteData_TARGE = Mid(EmoteData, InStr(EmoteData, "TARGE#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                            Exit For
                        End If
                    Next
                    tempvar = InStr(EmoteData, "PROPE#" + EmoteVIRID + "(")
                    For tempvar2 = InStr(EmoteData, "PROPE#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                        If Mid(EmoteData, tempvar2, 1) = ")" Then
                            EmoteData_PROPE = Mid(EmoteData, InStr(EmoteData, "PROPE#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                            Exit For
                        End If
                    Next
                    tempvar = InStr(EmoteData, "HASTA#" + EmoteVIRID + "(")
                    For tempvar2 = InStr(EmoteData, "HASTA#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                        If Mid(EmoteData, tempvar2, 1) = ")" Then
                            EmoteData_HASTA = Mid(EmoteData, InStr(EmoteData, "HASTA#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                            Exit For
                        End If
                    Next
                    If EmoteData_HASTA = 1 Then
                        tempvar = InStr(EmoteData, "TARGD#" + EmoteVIRID + "(")
                        For tempvar2 = InStr(EmoteData, "TARGD#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                            If Mid(EmoteData, tempvar2, 1) = ")" Then
                                EmoteData_TARGD = Mid(EmoteData, InStr(EmoteData, "TARGD#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                                Exit For
                            End If
                        Next
                        tempvar = InStr(EmoteData, "PERSD#" + EmoteVIRID + "(")
                        For tempvar2 = InStr(EmoteData, "PERSD#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                            If Mid(EmoteData, tempvar2, 1) = ")" Then
                                EmoteData_PERSD = Mid(EmoteData, InStr(EmoteData, "PERSD#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                                Exit For
                            End If
                        Next
                        tempvar = InStr(EmoteData, "TARGR#" + EmoteVIRID + "(")
                        For tempvar2 = InStr(EmoteData, "TARGR#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                            If Mid(EmoteData, tempvar2, 1) = ")" Then
                                EmoteData_TARGR = Mid(EmoteData, InStr(EmoteData, "TARGR#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                                Exit For
                            End If
                        Next
                    End If
                    tempvar = InStr(EmoteData, "ENDTM#" + EmoteVIRID + "(")
                    For tempvar2 = InStr(EmoteData, "ENDTM#" + EmoteVIRID + "(") To Len(EmoteData) Step 1
                        If Mid(EmoteData, tempvar2, 1) = ")" Then
                            EmoteData_ENDTM = Mid(EmoteData, InStr(EmoteData, "ENDTM#" + EmoteVIRID + "(") + 7 + Len(EmoteVIRID), tempvar2 - (tempvar + 7 + Len(EmoteVIRID)))
                            Exit For
                        End If
                    Next
                End If
            Else
                Debug.WriteLine("Error retrieving file: " & serr)
            End If
            If EmoteData_HASTA = 1 Then
                EmotePersD.Text = EmoteData_PERSD
                EmoteTargD.Text = EmoteData_TARGD
                Emotepropt.Text = EmoteData_TARGR
                EmotePersD.Enabled = True
                EmoteTargD.Enabled = True
                Emotepropt.Enabled = True
                UsesTargetCheck.Checked = True
            Else
                EmotePersD.Text = "Has no PersD"
                EmoteTargD.Text = "Has no TargD"
                Emotepropt.Text = "Has no PropT"
                EmotePersD.Enabled = False
                EmoteTargD.Enabled = False
                UsesTargetCheck.Checked = False
                Emotepropt.Enabled = False
            End If
            EmotePersE.Text = EmoteData_PERSE
            EmoteTargE.Text = EmoteData_TARGE
            EmoteProp.Text = EmoteData_PROPE
            Me.emote.Text = EmoteData_EMOTE
            example1.Text = "You " + EmotePersE.Text
            If EmoteData_HASTA = 1 Then
                example2.Text = "You " + EmotePersD.Text + " Target."
            Else
                example2.Text = "There is no targetable option."
            End If
            example3.Text = "Target " + EmoteTargE.Text
            If EmoteData_HASTA = 1 Then
                example4.Text = "Target " + EmoteTargD.Text + " Target2."
            Else
                example4.Text = "There is no targetable option."
            End If
            example5.Text = "Target " + EmoteProp.Text
            example6.Text = "Target " + Emotepropt.Text + " Target2."
            ENDTM.Text = Replace(EmoteData_ENDTM, "%p45", "")
        End If
    End Sub

    Private Sub createemote(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateBtn.Click
        Dim VNUM As String = LTrim(Str(emotelist.Items.Count + 1))
        EmoteData += Chr(13) + Chr(13)
        EmoteData += "VIRID#" + VNUM + "(" + VNUM + ")" + Chr(13)
        EmoteData += "EMOTE#" + VNUM + "(" + emote.Text + ")" + Chr(13)
        EmoteData += "PERSE#" + VNUM + "(" + EmotePersE.Text + ")" + Chr(13)
        EmoteData += "TARGE#" + VNUM + "(" + EmoteTargE.Text + ")" + Chr(13)
        EmoteData += "PROPE#" + VNUM + "(" + EmoteProp.Text + ")" + Chr(13)
        If UsesTargetCheck.Checked = True Then
            EmoteData += "HASTA#" + VNUM + "(1)" + Chr(13)
            EmoteData += "TARGD#" + VNUM + "(" + EmoteTargD.Text + ")" + Chr(13)
            EmoteData += "PERSD#" + VNUM + "(" + EmotePersD.Text + ")" + Chr(13)
            EmoteData += "TARGR#" + VNUM + "(" + Emotepropt.Text + ")" + Chr(13)
        Else
            EmoteData += "HASTA#" + VNUM + "(0)" + Chr(13)
        End If
        EmoteData += "ENDTM#" + VNUM + "(" + ENDTM.Text + ")"
        bans = SaveTextToFile(EmoteData, Debugger.DirEmotes, serr)
        loademotes(0, System.EventArgs.Empty)
        emotelist.SelectedIndex = VNUM - 1
        loademote(0, System.EventArgs.Empty)
    End Sub

    Private Sub savetheemote(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
        Dim VNUM As String = LTrim(Str(emotelist.SelectedIndex + 1))
        Dim NSERTEmoteData As String = "VIRID#" + VNUM + "(" + VNUM + ")" + Chr(13)
        NSERTEmoteData += "EMOTE#" + VNUM + "(" + emote.Text + ")" + Chr(13)
        NSERTEmoteData += "PERSE#" + VNUM + "(" + EmotePersE.Text + ")" + Chr(13)
        NSERTEmoteData += "TARGE#" + VNUM + "(" + EmoteTargE.Text + ")" + Chr(13)
        NSERTEmoteData += "PROPE#" + VNUM + "(" + EmoteProp.Text + ")" + Chr(13)
        If UsesTargetCheck.Checked = True Then
            NSERTEmoteData += "HASTA#" + VNUM + "(1)" + Chr(13)
            NSERTEmoteData += "TARGD#" + VNUM + "(" + EmoteTargD.Text + ")" + Chr(13)
            NSERTEmoteData += "PERSD#" + VNUM + "(" + EmotePersD.Text + ")" + Chr(13)
            NSERTEmoteData += "TARGR#" + VNUM + "(" + Emotepropt.Text + ")" + Chr(13)
        Else
            NSERTEmoteData += "HASTA#" + VNUM + "(0)" + Chr(13)
        End If
        NSERTEmoteData += "ENDTM#" + VNUM + "(" + ENDTM.Text + ")"
        Dim tmp0 As Integer, tmp1 As Integer, tmp2 As Integer
        For tmp0 = 1 To Len(EmoteData) Step 1
            If Mid(EmoteData, tmp0, Len("VIRID#(") + Len(LTrim(Str(emotelist.SelectedIndex)))) = "VIRID#" + LTrim(Str(emotelist.SelectedIndex + 1)) + "(" Then
                tmp1 = tmp0 - 1
            ElseIf Mid(EmoteData, tmp0, Len("ENDTM#(") + Len(LTrim(Str(emotelist.SelectedIndex)))) = "ENDTM#" + LTrim(Str(emotelist.SelectedIndex + 1)) + "(" Then
                tmp2 = tmp0 + Len("ENDTM#()" + EmoteData_ENDTM + LTrim(Str(emotelist.SelectedIndex))) + 1
                EmoteData = Mid(EmoteData, 1, tmp1) + NSERTEmoteData + Mid(EmoteData, tmp2 - 1, Len(EmoteData))
                Exit For
            End If
        Next
        bans = SaveTextToFile(EmoteData, Debugger.DirEmotes, serr)
        loademotes(0, System.EventArgs.Empty)
        emotelist.SelectedIndex = VNUM - 1
        loademote(0, System.EventArgs.Empty)
    End Sub
End Class